// ==UserScript==
// @name         js-studio
// @namespace    https://user-web.googlecode.com/
// @version      1
// @description  execute javascript
// @include      xhttps://localhost:44300/TestLive.htm
// @include      xhttps://www.google.com/calendar/render?tab=mc#g
// @version      1
// @grant        none
// @require      https://user-web.googlecode.com/svn/trunk/Scripts/jquery/jquery-2.1.0.js
// ==/UserScript==

"use strict";

try {
    var isTop = window.top == window.self;

    if (isTop) {
        var studio = new function () {
            var me = this;

            me.load = function () {
                me.url = document.location.href;
                me.isLinux = navigator.userAgent.indexOf("Linux") > -1;
                me.log("page url", me.url);
                //me.log("navigator.userAgent", navigator.userAgent);

                me.debug = { localUrls: 0, uniqueUrls: 0, reloader: 0, autoShow: 0 };

                if (me.url.match(/localhost|test\.htm/i) != null) {
                    me.debug = { localUrls: 0, uniqueUrls: 0, reloader: 0, autoShow: 0 };
                }

                if (navigator.userAgent.indexOf("Windows NT 6.3; WOW64") > -1
                 || navigator.userAgent.indexOf("Windows NT 6.1; WOW64") > -1) {
                    me.debug.localUrls = 1;
                }

                if (me.debug.reloader) {
                    /*$(document).dblclick(function () {
                    document.location.href = document.location.href;
                    });*/
                    document.oncontextmenu = function (e) {
                        document.location.reload(true);
                        return false;
                    };
                }

                me.outer = me
                    .add($("body"), "div")
                    .css({
                        position: "absolute",
                        zIndex: "9999999",
                        top: "100px",
                        left: "100px",
                        height: "200px",
                        width: "400px",
                        backgroundColor: "#fff",
                        border: "solid 1px #000",
                        padding: "10px",
                        color: "#000"
                    });

                me.code = me
                    .add(me.outer, "textarea")
                    .css({
                        width: "95%",
                        height: "95%"
                    })
                    .text("alert(22)")
                    .keyup(function (e) {
                        //me.log("key", e.keyCode);
                        if (e.keyCode == 59) {
                            try {
                                eval(this.value);
                            }
                            catch (ex) {
                                me.log("error", ex.message);
                            }
                        }
                    });
            };

            me.add = function (parent, tag) {
                var el = $("<" + tag + ">");
                parent.append(el);
                return el;
            };

            me.log = function (text1, text2) {
                log(text1, text2);
            };

        };

        window.setTimeout(function () {
            try {
                studio.load();
            }
            catch (ex) {
                log("error", ex.message);
            }
        }, 100);
    }
}
catch (ex) {
    if (ex.message.indexOf("accessing a cross-origin frame") > -1) {
        log("frame", ex.message);
    }
    else {
        log("error", ex.message);
    }
}

function log(text1, text2) {
    if (text2 != null) {
        if (text1.length < 20) {
            text1 = text1 + Array(21 - text1.length).join(" ");
        }

        if (text1.length > 20) {
            text1 = text1.substr(0, 20);
        }

        text1 = text1 + ": " + text2;
    }

    if (text1.length > 100) {
        text1 = text1.substr(0, 100) + "...";
    }

    console.log("[js-studio] " + text1);
};
